package com.xrkj.b2c.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <B>系统名称：</B><BR>
 * <B>模块名称：</B><BR>
 * <B>中文类名：</B>登录拦截器类<BR>
 * <B>概要说明：</B><BR>
 * 
 * @author 赵子靖
 * @since 2018年07月15日
 */
public class LoginInterceptor implements HandlerInterceptor {

    public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object obj, Exception arg3) throws Exception {

    }

    /**
     * 
     * <B>方法名称：</B>当前请求进行处理之后<BR>
     * <B>概要说明：</B>controller方法调用后执行<BR>
     * 
     * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse, java.lang.Object,
     *      org.springframework.web.servlet.ModelAndView)
     */
    public void postHandle(HttpServletRequest req, HttpServletResponse res, Object obj, ModelAndView arg3) throws Exception {

    }

    /**
     * 
     * <B>方法名称：</B>前置初始化操作<BR>
     * <B>概要说明：</B><BR>
     * 
     * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse, java.lang.Object)
     */
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object obj) throws Exception {
        if(this.JudgeIsMoblie(req)){
            //移动端
            HttpSession session = req.getSession();
            Object member = session.getAttribute("member");
            if(member==null){
                res.sendRedirect(req.getContextPath() + "/login/forwardMobileLogin.do");
                return false;
            }
        }else{
            //PC端
            HttpSession session = req.getSession();
            Object userName = session.getAttribute("userName");
            if(userName==null || StringUtil.isEmpty(userName.toString())){
                res.sendRedirect(req.getContextPath() + "/login/forwardLogin.do");
                return false;
            }
        }
        return true;
    }

    /**
     * 
     * <B>方法名称：</B>判断请求的类型<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author perry
     * @since 2017年12月11日
     * @param request
     * @return
     */
    public static boolean JudgeIsMoblie(HttpServletRequest request) {
        boolean isMoblie = false;
        String[] mobileAgents = { "iphone", "android", "ipad", "phone", "mobile", "wap", "netfront", "java", "opera mobi", "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony",
                "blackberry", "dopod", "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma", "docomo", "up.browser", "up.link", "blazer", "helio", "hosin",
                "huawei", "novarra", "coolpad", "webos", "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", "wellcom", "bunjalloo", "maui",
                "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220",
                "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs",
                "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant",
                "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli",
                "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-", "Googlebot-Mobile" };
        if (request.getHeader("User-Agent") != null) {
            String agent = request.getHeader("User-Agent");
            for (String mobileAgent : mobileAgents) {
                if (agent.toLowerCase().indexOf(mobileAgent) >= 0 && agent.toLowerCase().indexOf("windows nt") <= 0 && agent.toLowerCase().indexOf("macintosh") <= 0) {
                    isMoblie = true;
                    break;
                }
            }
        }
        return isMoblie;
    }

}
